package org.hank.compose_yyzs.viewmodel

import androidx.constraintlayout.compose.ConstraintSet
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.hank.compose_yyzs.bean.BuildingBean
import org.hank.compose_yyzs.bean.ProjectTreeNode
import org.hank.compose_yyzs.bean.request.BuildingPagerRequest
import org.hank.compose_yyzs.bean.request.BuildingRoomsCountRequest
import org.hank.compose_yyzs.constants.Constants
import org.hank.compose_yyzs.http.ApiService
import org.hank.compose_yyzs.ui.paging.simplePager
import javax.inject.Inject
import kotlin.random.Random

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/11/17 15:46
 *描述:
 */
@HiltViewModel
class BuildingViewModel @Inject constructor(
    var apiService: ApiService,
    private val savedStateHandle: SavedStateHandle
) : ViewModel() {
    var isRefresh = MutableStateFlow(false)
    var projectName = MutableStateFlow("")
    var currentStageName = MutableStateFlow("")
    var currentStage = MutableStateFlow(0)
    val pager by lazy {
        getBuildingPager(savedStateHandle.get<Int>("projectId")?:0)
    }

    init {
        viewModelScope.launch {
            currentStage.emit(MMKV.defaultMMKV().getInt(Constants.CURRENT_STAGE, 0))
            var name = MMKV.defaultMMKV().getString(Constants.CURRENT_PROJECT_NAME, "")
            var projectTreeStr = MMKV.defaultMMKV().getString(Constants.CURRENT_PROJECTTREE, "")
            if (!projectTreeStr.isNullOrEmpty()) {
                var projectTree = Gson().fromJson(projectTreeStr, ProjectTreeNode::class.java)
                projectName.emit(projectTree.name + name)
            } else {
                projectName.emit(name ?: "")
            }
        }
    }


    private fun getBuildingPager(projectId: Int): Flow<PagingData<BuildingBean>> {
        return simplePager(isRefresh,
            secondAction = {
                apiService.getBuildingRoomsCountByCode(
                    BuildingRoomsCountRequest(
                        it.asSequence().map { it.id }.toMutableList(), currentStage.value
                    )
                )
            },
            secondMerge = { list1, list2 ->
                list1.forEach { buildingBean ->
                    list2.forEach { stageData ->
                        if (stageData.blockId == buildingBean.id) {
                            stageData.totalRoomCount = 5
                            buildingBean.stageData = stageData
                            buildingBean.status = stageData.status
                        }
                    }
                }
                list1
            }
        ) {
            apiService.getBuildingPager(BuildingPagerRequest(current = it, projectId = projectId))
        }
    }


}